from rest_framework import serializers
from . import models

# 部门表序列化
class DepartmentSerializer(serializers.ModelSerializer):

    class Meta:
        model=models.Department
        fields=('id','name')



# 部门表反序列化
class DepartmentUnserializer(serializers.Serializer):
    # 约束
    name=serializers.CharField(max_length=32)

    def create(self, validated_data):
        return models.Department.objects.create(**validated_data)




#员工表序列化
class StaffSerializer(serializers.ModelSerializer):
    cate_name=serializers.CharField(source='cate.name')
    class Meta:
        model=models.Staff
        fields='__all__'


# 员工表反序列化
class StaffUnserializer(serializers.Serializer):
    #约束
    username=serializers.CharField(max_length=32)
    password=serializers.CharField(max_length=255)
    cate_id=serializers.IntegerField()

    def create(self, validated_data):
        return models.Staff.objects.create(**validated_data)